package com.matt.service.impl;

import com.matt.commons.dto.sso.StaffAreaDTO;
import com.matt.domain.Classes;
import com.matt.domain.NoticeMessage;
import com.matt.domain.course.CoursePlanClasses;
import com.matt.domain.nursery.NurseryScheduleCheck;
import com.matt.domain.rel.RelClassesTeacher;
import com.matt.foundation.DO.course.NoticeCoursePlanDO;
import com.matt.foundation.constant.CourseConstant;
import com.matt.foundation.constant.NoticeMessageConstant;
import com.matt.foundation.query.NoticeCoursePlanQuery;
import com.matt.foundation.query.NoticeQuery;
import com.matt.foundation.utils.DateUtil;
import com.matt.foundation.vo.NoticeMessageVo;
import com.matt.repository.ClassesRepository;
import com.matt.repository.NoticeMessageRepository;
import com.matt.repository.base.ClassesCoursePlanRepository;
import com.matt.repository.base.NoticeMessageNativeRepository;
import com.matt.repository.course.CoursePlanClassesRepository;
import com.matt.repository.nursery.NurseryScheduleCheckRepository;
import com.matt.repository.rel.RelClassesTeacherRepository;
import com.matt.service.CourseAuditService;
import com.matt.service.NoticeMessageService;
import com.matt.service.remoteCall.SsoCallService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.matt.foundation.constant.CommonConstant.*;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2018/3/23
 * *******************************
 */
@Service
@Slf4j
public class NoticeMessageServiceImpl implements NoticeMessageService {

    @Autowired
    private NoticeMessageRepository noticeMessageRepository;

    @Autowired
    private NoticeMessageNativeRepository noticeMessageNativeRepository;

    @Autowired
    private ClassesCoursePlanRepository classesCoursePlanRepository;

    @Autowired
    private RelClassesTeacherRepository relClassesTeacherRepository;

    @Autowired
    private ClassesRepository classesRepository;

    @Autowired
    private NurseryScheduleCheckRepository nurseryScheduleCheckRepository;

    @Autowired
    private CoursePlanClassesRepository coursePlanClassesRepository;

    @Autowired
    private CourseAuditService courseAuditService;

    @Autowired
    private SsoCallService ssoCallService;


   /* @Value("${website.server.url}")
    private String WEBSITE_URL;
*/
    @Override
    public List<NoticeMessageVo> getNoticeList(String staffId, String areaId) {
        List<NoticeMessage> messageList = noticeMessageRepository.findByReceiveIdAndDueStatusAndAreaId(staffId, NoticeMessageConstant.DueStatus.NON_DUE.codeOf(), areaId);
        List<NoticeMessageVo> voList = messageList.stream().map(o -> replace(o)).collect(Collectors.toList());
        return voList;
    }

    private NoticeMessageVo replace(NoticeMessage noticeMessage){
     /*   if (noticeMessage.getCategory() == NoticeMessageConstant.Category.THEME_COURSE_PREPARE.codeOf() ||
            noticeMessage.getCategory() == NoticeMessageConstant.Category.EARLY_COURSE_PREPARE.codeOf() ||
            noticeMessage.getCategory() == NoticeMessageConstant.Category.THEME_COURSE_REJECTED.codeOf() ||
            noticeMessage.getCategory() == NoticeMessageConstant.Category.EARLY_COURSE_REJECTED.codeOf() ||
            noticeMessage.getCategory() == NoticeMessageConstant.Category.THEME_COURSE_AUDIT.codeOf() ||
            noticeMessage.getCategory() == NoticeMessageConstant.Category.EARLY_COURSE_AUDIT.codeOf() ){
            String url = String.format(NoticeMessageConstant.Category.NURSERY_AUDITOR.get(noticeMessage.getCategory()).urlOf(), WEBSITE_URL, noticeMessage.getLinkId());
            return NoticeMessageVo.of(noticeMessage, url);
        }
        if (noticeMessage.getCategory() == NoticeMessageConstant.Category.LIVE_PLAN_PREPARE.codeOf() ||
            noticeMessage.getCategory() == NoticeMessageConstant.Category.LIVE_PLAN_AUDIT.codeOf() ||
            noticeMessage.getCategory() == NoticeMessageConstant.Category.LIVE_PLAN_REJECTED.codeOf() ){
            String url = String.format(NoticeMessageConstant.Category.NURSERY_AUDITOR.get(noticeMessage.getCategory()).urlOf(), WEBSITE_URL, noticeMessage.getLinkId(), noticeMessage.getExt());
            return NoticeMessageVo.of(noticeMessage, url);
        }

        if (noticeMessage.getCategory() == NoticeMessageConstant.Category.NURSERY_AUDITOR.codeOf() ||
                noticeMessage.getCategory() == NoticeMessageConstant.Category.EARLY_AUDITOR.codeOf() ||
                noticeMessage.getCategory() == NoticeMessageConstant.Category.LIVE_AUDITOR.codeOf() ){
            String url = String.format(NoticeMessageConstant.Category.NURSERY_AUDITOR.get(noticeMessage.getCategory()).urlOf(), WEBSITE_URL);
            return NoticeMessageVo.of(noticeMessage, url);
        }*/
        return null;
    }

    @Override
    public Page<NoticeMessageVo> getNoticeMessageHistory(NoticeQuery query) {
        List<NoticeMessage> messageList = noticeMessageNativeRepository.findByConditions(query);
        List<NoticeMessageVo> voList = messageList.stream().map(o -> replace(o)).collect(Collectors.toList());
        Long count = noticeMessageNativeRepository.countByConditions(query);
        return new PageImpl<NoticeMessageVo>(voList, PageRequest.of(query.getPageNum(), query.getPageSize()), count);
    }

    @Override
    public Long countNoticeMessageHistory(NoticeQuery query) {
       return noticeMessageNativeRepository.countByConditions(query);
    }

    @Transactional
    @Override
    public boolean deleteByLinkIdAndCategory(String linkId, Integer category) {
        noticeMessageRepository.deleteByLinkIdAndCategory(linkId, category);
        return true;
    }

    @Transactional
    @Override
    public Long deleteNurseryCourseByLinkIds(List<String> linkIds){
        return noticeMessageRepository.deleteByLinkIdInAndCategoryIn(linkIds, Lists.newArrayList(NoticeMessageConstant.Category.THEME_COURSE_PREPARE.codeOf(), NoticeMessageConstant.Category.THEME_COURSE_REJECTED.codeOf(), NoticeMessageConstant.Category.THEME_COURSE_AUDIT.codeOf()));
    }

    @Transactional
    @Override
    public boolean deleteByLinkId(String linkId) {
        noticeMessageRepository.deleteByLinkId(linkId);
        return true;
    }

    @Override
    public boolean generateCourseAuditor() {
    	//获取园区列表
        // TODO:  需要处理这个
//        List<AreaDTO> areaList = ssoCallService.getAllArea();
//        for (AreaDTO area:areaList){
//        	//获取园区员工信息和课程设置信息
//            Map map = courseAuditService.getAuditor(area.getId());
//            //通过平台，园区ID，角色获取员工列表信息
//            List<StaffDTO> staffList = ssoCallService.getAreaStaffByRole(null, "teach::nursery::course_audit::action", null);
//            //保存通知消息
//            if (StringUtils.isBlank(map.get("nurseryTeacherId").toString()))
//                generateAuditor(staffList, NoticeMessageConstant.Category.NURSERY_AUDITOR);
//
//            if (StringUtils.isBlank(map.get("earlyTeacherId").toString()))
//                generateAuditor(staffList, NoticeMessageConstant.Category.EARLY_AUDITOR);
//
//            if (StringUtils.isBlank(map.get("liveTeacherId").toString()))
//                generateAuditor(staffList, NoticeMessageConstant.Category.EARLY_AUDITOR);
//        }
        return false;
    }

    private boolean generateAuditor(List<StaffAreaDTO> staffList, NoticeMessageConstant.Category category){
        for (StaffAreaDTO staff:staffList){
            NoticeMessage notice = noticeMessageRepository.findByReceiveIdAndCategory(staff.getId(), category.codeOf());
            if (Objects.isNull(notice)){
                notice = NoticeMessage.of(category.messageOf(), category, staff.getAreaId(), staff.getId(), staff.getAreaId());
                noticeMessageRepository.save(notice);
            }
        }
        return true;
    }

    @Transactional
    @Override
    public boolean generateThemeCoursePrepare(){
        NoticeCoursePlanQuery query = new NoticeCoursePlanQuery();
        query.setBeginDate(DateUtil.millisToDateTime(System.currentTimeMillis(), "yyyy-MM-dd"));
        query.setPrepareStatus(CourseConstant.PrepareStatus.NON_PREPARE.codeOf());
        query.setCategory(CourseConstant.Category.NURSERY.codeOf());
        List<NoticeCoursePlanDO> coursePlanClasses = classesCoursePlanRepository.findNoticeCoursePlan(query);
        for (NoticeCoursePlanDO plan : coursePlanClasses){
            List<RelClassesTeacher> classesTeacherList = relClassesTeacherRepository.findByClassesId(plan.getClassesId());
            for (RelClassesTeacher rel : classesTeacherList){
                String message = String.format(NoticeMessageConstant.Category.THEME_COURSE_PREPARE.messageOf(), plan.getClassesName(), DateUtil.convertStr(plan.getCourseDate()), plan.getCourseName());
                NoticeMessage noticeMessage =  NoticeMessage.of(message, NoticeMessageConstant.Category.THEME_COURSE_PREPARE, plan.getCoursePlanClassesId(), rel.getStaffId(), plan.getAreaId());
                noticeMessage.setDueDate(plan.getCourseDate());
                refreshCourseNotice(noticeMessage);
            }
        }
        //自动审核通过离课程时间小于一天的备课课程
        List<CoursePlanClasses> nonAuditcoursePlanClasses=coursePlanClassesRepository.findByPrepareStatus(CourseConstant.PrepareStatus.NON_AUDIT.codeOf());
        for (CoursePlanClasses plan : nonAuditcoursePlanClasses){
        	courseAuditService.auditByCoursePlanClasses(plan);
        }
        return true;
    }

    @Transactional
    @Override
    public boolean generateThemeCourseRejected(String coursePlanClassesId){
        //删除原有消息
        deleteByLinkIdAndCategory(coursePlanClassesId, NoticeMessageConstant.Category.THEME_COURSE_AUDIT.codeOf());
        //生成新消息
        CoursePlanClasses plan = coursePlanClassesRepository.getOne(coursePlanClassesId);
        Classes classes = classesRepository.getOne(plan.getClassesId());
        List<RelClassesTeacher> classesTeacherList = relClassesTeacherRepository.findByClassesId(plan.getClassesId());
        for (RelClassesTeacher rel : classesTeacherList){
            String message = String.format(NoticeMessageConstant.Category.THEME_COURSE_AUDIT.messageOf(), classes.getName(), DateUtil.convertStr(plan.getCourseDate()), plan.getCourseName());
            NoticeMessage noticeMessage =  NoticeMessage.of(message, NoticeMessageConstant.Category.THEME_COURSE_AUDIT, plan.getId(), rel.getStaffId(), plan.getAreaId());
            noticeMessage.setDueDate(plan.getCourseDate());
            refreshCourseNotice(noticeMessage);
        }
        return true;
    }


    @Transactional
    @Override
    public boolean generateThemeCourseAudit(String coursePlanClassesId) {
        //删除原有消息
        deleteByLinkIdAndCategory(coursePlanClassesId, NoticeMessageConstant.Category.THEME_COURSE_PREPARE.codeOf());
        deleteByLinkIdAndCategory(coursePlanClassesId, NoticeMessageConstant.Category.THEME_COURSE_REJECTED.codeOf());
        //生成新消息
        CoursePlanClasses plan = coursePlanClassesRepository.getOne(coursePlanClassesId);
        Classes classes = classesRepository.getOne(plan.getClassesId());
        String auditor = courseAuditService.getAuditor(classes.getAreaId(), NURSERY_AUDITOR_OPEN_KEY).get("auditor").toString();
        if (StringUtils.isNotBlank(auditor)){
            String message = String.format(NoticeMessageConstant.Category.THEME_COURSE_AUDIT.messageOf(), classes.getName(), DateUtil.convertStr(plan.getCourseDate()), plan.getCourseName());
            NoticeMessage noticeMessage =  NoticeMessage.of(message, NoticeMessageConstant.Category.THEME_COURSE_AUDIT, plan.getId(), auditor, plan.getAreaId());
            noticeMessage.setDueDate(plan.getCourseDate());
            refreshCourseNotice(noticeMessage);
        }
        return true;
    }

    @Transactional
    @Override
    public boolean generateEarlyCoursePrepare(){
        NoticeCoursePlanQuery query = new NoticeCoursePlanQuery();
        query.setBeginDate(DateUtil.millisToDateTime(System.currentTimeMillis(), "yyyy-MM-dd"));
        query.setPrepareStatus(CourseConstant.PrepareStatus.NON_PREPARE.codeOf());
        query.setCategory(CourseConstant.Category.EARLY.codeOf());
        List<NoticeCoursePlanDO> coursePlanClasses = classesCoursePlanRepository.findNoticeCoursePlan(query);
        for (NoticeCoursePlanDO plan : coursePlanClasses){
            List<RelClassesTeacher> classesTeacherList = relClassesTeacherRepository.findByClassesId(plan.getClassesId());
            for (RelClassesTeacher rel : classesTeacherList){
                String message = String.format(NoticeMessageConstant.Category.EARLY_COURSE_PREPARE.messageOf(), plan.getClassesName(), DateUtil.convertStr(plan.getCourseDate()), plan.getCourseName());
                NoticeMessage noticeMessage =  NoticeMessage.of(message, NoticeMessageConstant.Category.EARLY_COURSE_PREPARE, plan.getCoursePlanClassesId(), rel.getStaffId(), plan.getAreaId());
                noticeMessage.setDueDate(plan.getCourseDate());
                refreshCourseNotice(noticeMessage);
            }
        }
        return true;
    }

    @Transactional
    @Override
    public boolean generateEarlyCourseRejected(String coursePlanClassesId){
        //删除原有消息
        deleteByLinkIdAndCategory(coursePlanClassesId, NoticeMessageConstant.Category.EARLY_COURSE_AUDIT.codeOf());
        //生成新消息
        CoursePlanClasses plan = coursePlanClassesRepository.getOne(coursePlanClassesId);
        Classes classes = classesRepository.getOne(plan.getClassesId());
        List<RelClassesTeacher> classesTeacherList = relClassesTeacherRepository.findByClassesId(plan.getClassesId());
        for (RelClassesTeacher rel : classesTeacherList){
            String message = String.format(NoticeMessageConstant.Category.EARLY_COURSE_REJECTED.messageOf(), classes.getName(), DateUtil.convertStr(plan.getCourseDate()), plan.getCourseName());
            NoticeMessage noticeMessage =  NoticeMessage.of(message, NoticeMessageConstant.Category.EARLY_COURSE_REJECTED, plan.getId(), rel.getStaffId(), plan.getAreaId());
            noticeMessage.setDueDate(plan.getCourseDate());
            refreshCourseNotice(noticeMessage);
        }
        return true;
    }


    @Transactional
    @Override
    public boolean generateEarlyCourseAudit(String coursePlanClassesId) {
        //删除原有消息
        deleteByLinkIdAndCategory(coursePlanClassesId, NoticeMessageConstant.Category.EARLY_COURSE_PREPARE.codeOf());
        deleteByLinkIdAndCategory(coursePlanClassesId, NoticeMessageConstant.Category.EARLY_COURSE_REJECTED.codeOf());
        //生成新消息
        CoursePlanClasses plan = coursePlanClassesRepository.getOne(coursePlanClassesId);
        Classes classes = classesRepository.getOne(plan.getClassesId());
        String auditor = courseAuditService.getAuditor(classes.getAreaId(), EARLY_AUDITOR_OPEN_KEY).get("auditor").toString();
        if (StringUtils.isNotBlank(auditor)){
            String message = String.format(NoticeMessageConstant.Category.EARLY_COURSE_AUDIT.messageOf(), classes.getName(), DateUtil.convertStr(plan.getCourseDate()), plan.getCourseName());
            NoticeMessage noticeMessage =  NoticeMessage.of(message, NoticeMessageConstant.Category.EARLY_COURSE_AUDIT, plan.getId(), auditor, plan.getAreaId());
            noticeMessage.setDueDate(plan.getCourseDate());
            refreshCourseNotice(noticeMessage);
        }
        return true;
    }

    @Transactional
    @Override
    public boolean generateAreaBuildCourseAudit(String coursePlanClassesId) {
        //删除原有消息
        deleteByLinkIdAndCategory(coursePlanClassesId, NoticeMessageConstant.Category.AREABUILD_COURSE_PREPARE.codeOf());
        deleteByLinkIdAndCategory(coursePlanClassesId, NoticeMessageConstant.Category.AREABUILD_COURSE_REJECTED.codeOf());
        //生成新消息
        CoursePlanClasses plan = coursePlanClassesRepository.getOne(coursePlanClassesId);
        Classes classes = classesRepository.getOne(plan.getClassesId());
        String auditor = courseAuditService.getAuditor(classes.getAreaId(), AREA_BUILD_OPEN_KEY).get("auditor").toString();
        if (StringUtils.isNotBlank(auditor)){
            String message = String.format(NoticeMessageConstant.Category.AREABUILD_COURSE_AUDIT.messageOf(), classes.getName(), DateUtil.convertStr(plan.getCourseDate()), plan.getCourseName());
            NoticeMessage noticeMessage =  NoticeMessage.of(message, NoticeMessageConstant.Category.AREABUILD_COURSE_AUDIT, plan.getId(), auditor, plan.getAreaId());
            noticeMessage.setDueDate(plan.getCourseDate());
            refreshCourseNotice(noticeMessage);
        }
        return true;
    }

    private boolean refreshCourseNotice(NoticeMessage noticeMessage){
        NoticeMessage notice = noticeMessageRepository.findByReceiveIdAndCategoryAndLinkId(noticeMessage.getReceiveId(), noticeMessage.getCategory(), noticeMessage.getLinkId());
        if (Objects.isNull(notice)){
            notice = noticeMessage;
        }else {
            notice.setLastModified(new Date());
        }
        //TODO:没有主键要限制录入
        noticeMessageRepository.save(notice);
        return true;
    }


    @Override
    public boolean generateLivePlanPrepare() {
        List<Classes> classesList = classesRepository.findByCategoryAndDeleted(CourseConstant.Category.NURSERY.codeOf(), false);
        Date nextWeekDay = new Date(DateUtil.plusDays(System.currentTimeMillis(), 7L));
        Date beginDate = DateUtil.getWeekBegin(nextWeekDay);
        Date endDate = DateUtil.getWeekEnd(nextWeekDay);

        for (Classes classes : classesList){
            NurseryScheduleCheck check = nurseryScheduleCheckRepository.findByClassesIdAndBeginDateAndEndDate(classes.getId(), beginDate, endDate);
            List<RelClassesTeacher> classesTeacherList = relClassesTeacherRepository.findByClassesId(classes.getId());
            if (Objects.nonNull(check)){
                for (RelClassesTeacher rel:classesTeacherList){
                    String message = String.format(NoticeMessageConstant.Category.LIVE_PLAN_PREPARE.messageOf(), classes.getName());
                    NoticeMessage noticeMessage =  NoticeMessage.of(message, NoticeMessageConstant.Category.LIVE_PLAN_PREPARE, classes.getId(), rel.getStaffId(), classes.getAreaId());
                    noticeMessage.setExt(DateUtil.convertStr(check.getBeginDate()));
                    noticeMessage.setDueDate(endDate);
                    refreshLivePlanNotice(noticeMessage, beginDate, endDate);
                }
            }
        }
        return false;
    }

    private boolean refreshLivePlanNotice(NoticeMessage noticeMessage, Date begin, Date end){
        NoticeMessage notice = noticeMessageRepository.findByReceiveIdAndCategoryAndLinkIdAndCreatedBetween(noticeMessage.getReceiveId(), noticeMessage.getCategory(), noticeMessage.getLinkId(), begin, end);
        if (Objects.isNull(notice)){
            notice = noticeMessage;
        }else {
            notice.setLastModified(new Date());
        }
        //TODO:没有主键要限制录入
        noticeMessageRepository.save(notice);
        return true;
    }

    @Transactional
    @Override
    public boolean generateLivePlanRejected(String checkId) {
        NurseryScheduleCheck check = nurseryScheduleCheckRepository.getOne(checkId);
        Classes classes = classesRepository.getOne(check.getClassesId());
        //删除原有消息
        noticeMessageRepository.deleteByLinkIdAndCategory(check.getClassesId(), NoticeMessageConstant.Category.LIVE_PLAN_AUDIT.codeOf());
        //生成新消息
        String date = String.format("%s-%s", DateUtil.millisToDateTime(check.getBeginDate().getTime(), "MM月dd日"), DateUtil.millisToDateTime(check.getEndDate().getTime(), "MM月dd日"));
        List<RelClassesTeacher> classesTeacherList = relClassesTeacherRepository.findByClassesId(classes.getId());
        for (RelClassesTeacher rel:classesTeacherList){
            String message = String.format(NoticeMessageConstant.Category.LIVE_PLAN_REJECTED.messageOf(), classes.getName(), date);
            NoticeMessage noticeMessage =  NoticeMessage.of(message, NoticeMessageConstant.Category.LIVE_PLAN_REJECTED, classes.getId(), rel.getStaffId(), classes.getAreaId());
            noticeMessage.setExt(DateUtil.convertStr(check.getBeginDate()));
            noticeMessage.setDueDate(check.getEndDate());
            refreshCourseNotice(noticeMessage);
        }
        return true;
    }

    @Transactional
    @Override
    public boolean generateLivePlanAudit(String checkId) {
        NurseryScheduleCheck check = nurseryScheduleCheckRepository.getOne(checkId);
        Classes classes = classesRepository.getOne(check.getClassesId());
        //删除原有消息
        noticeMessageRepository.deleteByLinkIdAndCategory(check.getClassesId(), NoticeMessageConstant.Category.LIVE_PLAN_PREPARE.codeOf());
        noticeMessageRepository.deleteByLinkIdAndCategory(check.getClassesId(), NoticeMessageConstant.Category.LIVE_PLAN_REJECTED.codeOf());
        //生成新消息
        String date = String.format("%s-%s", DateUtil.millisToDateTime(check.getBeginDate().getTime(), "MM月dd日"), DateUtil.millisToDateTime(check.getEndDate().getTime(), "MM月dd日"));
        List<RelClassesTeacher> classesTeacherList = relClassesTeacherRepository.findByClassesId(classes.getId());
        String auditor = courseAuditService.getAuditor(classes.getAreaId(), LIVE_AUDITOR_OPEN_KEY).get("auditor").toString();
        if (StringUtils.isNotBlank(auditor)){
            String message = String.format(NoticeMessageConstant.Category.LIVE_PLAN_AUDIT.messageOf(), classes.getName(), date);
            NoticeMessage noticeMessage =  NoticeMessage.of(message, NoticeMessageConstant.Category.LIVE_PLAN_AUDIT, classes.getId(), auditor, classes.getAreaId());
            noticeMessage.setExt(DateUtil.convertStr(check.getBeginDate()));
            noticeMessage.setDueDate(check.getEndDate());
            refreshCourseNotice(noticeMessage);
        }
        return true;
    }

    @Transactional
    @Override
    public boolean overdueProcess(Date date) {
        noticeMessageRepository.updateByDueDate(date, NoticeMessageConstant.DueStatus.DUE.codeOf());
        return false;
    }

	@Override
	public boolean generateKXThemeCourseAudit(String coursePlanClassesId) {
		 //删除原有消息
        deleteByLinkIdAndCategory(coursePlanClassesId, NoticeMessageConstant.Category.KXTHEME_COURSE_PREPARE.codeOf());
        deleteByLinkIdAndCategory(coursePlanClassesId, NoticeMessageConstant.Category.KXTHEME_COURSE_REJECTED.codeOf());
        //生成新消息
        CoursePlanClasses plan = coursePlanClassesRepository.getOne(coursePlanClassesId);
        Classes classes = classesRepository.getOne(plan.getClassesId());
        String auditor = courseAuditService.getAuditor(classes.getAreaId(), KXTHEME_AUDITOR_OPEN_KEY).get("auditor").toString();
        if (StringUtils.isNotBlank(auditor)){
            String message = String.format(NoticeMessageConstant.Category.KXTHEME_COURSE_AUDIT.messageOf(), classes.getName(), DateUtil.convertStr(plan.getCourseDate()), plan.getCourseName());
            NoticeMessage noticeMessage =  NoticeMessage.of(message, NoticeMessageConstant.Category.KXTHEME_COURSE_AUDIT, plan.getId(), auditor, plan.getAreaId());
            noticeMessage.setDueDate(plan.getCourseDate());
            refreshCourseNotice(noticeMessage);
        }
        return true;
	}

	 @Transactional
	    @Override
	    public boolean generateKXThemeCourseRejected(String coursePlanClassesId){
	        //删除原有消息
	        deleteByLinkIdAndCategory(coursePlanClassesId, NoticeMessageConstant.Category.KXTHEME_COURSE_AUDIT.codeOf());
	        //生成新消息
	        CoursePlanClasses plan = coursePlanClassesRepository.getOne(coursePlanClassesId);
	        Classes classes = classesRepository.getOne(plan.getClassesId());
	        List<RelClassesTeacher> classesTeacherList = relClassesTeacherRepository.findByClassesId(plan.getClassesId());
	        for (RelClassesTeacher rel : classesTeacherList){
	            String message = String.format(NoticeMessageConstant.Category.KXTHEME_COURSE_AUDIT.messageOf(), classes.getName(), DateUtil.convertStr(plan.getCourseDate()), plan.getCourseName());
	            NoticeMessage noticeMessage =  NoticeMessage.of(message, NoticeMessageConstant.Category.KXTHEME_COURSE_AUDIT, plan.getId(), rel.getStaffId(), plan.getAreaId());
	            noticeMessage.setDueDate(plan.getCourseDate());
	            refreshCourseNotice(noticeMessage);
	        }
	        return true;
	    }
}
